package cn.kow.kmall.service.impl;

import cn.kow.kmall.common.constant.KmallConstants;
import cn.kow.kmall.core.constant.KmallStatusConstant;
import cn.kow.kmall.core.exception.ServiceException;
import cn.kow.kmall.dao.ItemCategoryGroupRepository;
import cn.kow.kmall.dao.ItemCategoryImageRepository;
import cn.kow.kmall.dao.ItemCategoryRepository;
import cn.kow.kmall.pojo.entity.ItemCategory;
import cn.kow.kmall.pojo.entity.ItemCategoryGroup;
import cn.kow.kmall.pojo.entity.ItemCategoryImage;
import cn.kow.kmall.service.ItemCategoryService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: lijun
 * @Description:
 * @Date: Created in 2019-05-24 17:24
 * @Modified By:
 */
@Service("itemCategoryService")
public class ItemCategoryServiceImpl implements ItemCategoryService {

    @Autowired
    private ItemCategoryRepository categoryRepository;

    @Autowired
    private ItemCategoryImageRepository categoryImageRepository;

    @Autowired
    private ItemCategoryGroupRepository categoryGroupRepository;

    @Override
    public List<ItemCategory> listByParentId(Long parentId) {
        ItemCategory record = new ItemCategory();
        record.setStatus(KmallStatusConstant.NORMAL);
        record.setPid(parentId);
        List<ItemCategory> list = categoryRepository.listAll(record, Sort.by(Sort.Direction.ASC, "sort"));
        return list;
    }

    @Override
    public ItemCategory getById(Long id) {
        ItemCategory itemCategory = categoryRepository.getOne(id);
        return itemCategory;
    }

    public ItemCategory getByCode(String code) {
        ItemCategory record = new ItemCategory();
        record.setCode(code);
        record.setDeleted(false);
        record.setStatus(KmallStatusConstant.NORMAL);
        ItemCategory itemCategory = categoryRepository.findOne(Example.of(record)).orElse(null);
        return itemCategory;
    }

    @Override
    public List<ItemCategoryGroup> listCategoryGroup(List<Long> groupIdList) {
        return categoryGroupRepository.findAllById(groupIdList);
    }

    @Override
    public Map<Long, ItemCategory> listForMap() {
        ItemCategory record = new ItemCategory();
        record.setStatus(KmallStatusConstant.NORMAL);
        List<ItemCategory> list = categoryRepository.listAll(record);
        if (CollectionUtils.isEmpty(list)) {
            return MapUtils.EMPTY_MAP;
        }
        return list.stream().collect(Collectors.toMap(ItemCategory::getId, a -> a, (k1, k2) -> k1));
    }

    @Override
    public List<ItemCategoryImage> listCategoryImage(Long id) {
        ItemCategoryImage record = new ItemCategoryImage();
        record.setCategoryId(id);
        record.setStatus(KmallStatusConstant.NORMAL);
        return categoryImageRepository.listAll(record, Sort.by(Sort.Direction.ASC, "sort"));
    }

    @Override
    public void save(ItemCategory category) {
        if (category.getType() == 1) {
            category.setPid(0L);
        } else if (null == category.getPid() || 0 == category.getPid()) {
            throw new ServiceException(KmallConstants.FAIL, "上级类目不能为空");
        }
        String code = category.getCode();
        if (getByCode(code) != null) {
            throw new ServiceException(KmallConstants.FAIL, "类目编码已存在");
        }
        if (StringUtils.isBlank(category.getName())) {
            throw new ServiceException(KmallConstants.FAIL, "类目名称不能为空");
        }
        categoryRepository.create(category);
    }

    @Override
    public void remove(List<Long> idList) {
        categoryRepository.deleteAllById(idList);
    }

    @Override
    public void update(ItemCategory category) {
        category.getId();
        String code = category.getCode();
        ItemCategory tmp = getByCode(code);
        if (tmp != null && !category.getId().equals(tmp.getId())) {
            throw new ServiceException(KmallConstants.FAIL, "类目编码已存在");
        }
        categoryRepository.update(category);
    }
}
